package com.qf.servlet;

import com.qf.entity.Admin;
import com.qf.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet(urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {



    //用来处理任何类型的请求 POST GET PUT
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }
    //专门处理post
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        String againpassword=req.getParameter("againpassword");
        String name =req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String [] powers =req.getParameterValues("power");
        String jiguan = req.getParameter("jiguan");

        System.out.println(username+""+password+""+againpassword+""+name+""+
                sex+""+birthday+""+ Arrays.toString(powers) +""+jiguan+"");

        if(password ==null || password.equals("") || !password.equals(againpassword)){
            //密码输入有问题
            //两次密码输入异常
            resp.sendRedirect("register.jsp?status=1");
            return;
        }
        if(username ==null || username.equals("")){
            //用户名不能为空
            resp.sendRedirect("register.jsp?status=2");
            return;
        }

        //将注册信息转换成实体类
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setJiguan(jiguan);
        admin.setSex(sex);
        admin.setBrithday(birthday);
        admin.setPower(powers);
        //存储用户的注册数据 --存储到数据库 -list集合中
        DateUtils.addAdmin(admin);
        //注册成功 跳转到登录页面
        resp.sendRedirect("register.jsp?status=3");

    }
}
